//
//  WBNetworkManager+Extension.swift
//  XLWB
//
//  Created by MacBook on 16/7/2.
//  Copyright © 2016年 MacBook. All rights reserved.
//

import Foundation

//MARK--封装新浪微博的网络请求方法
extension WBNetworkManager {

    //加载微博的网络请求方法
    

    func statusList(since_id: Int64 = 0, max_id: Int64 = 0, completion: (list: [[String: AnyObject]]?, isSuccess: Bool)->()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params = ["since_id": "\(since_id)",
                      "max_id": "\(max_id > 0 ? max_id - 1 : 0)"]

        
        tokenRequest(URLString: urlString, parameters: params){ (json, isSuccess) in
            //从json中获取statuses字典数组
            //ruguo as? 失败,result = nil
            let result = json?["statuses"] as? [[String: AnyObject]]
            
            completion(list: result,isSuccess: isSuccess)
        
        }
    }
    
    
    //返回微博未读数量
    func unreadCount(completion: (count: Int)->()) {
    
        guard let uid = userAccount.uid else {
            return
        }
    
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params = ["uid": uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
        
            let dict = json as? [String: AnyObject]
            let count = dict?["status"] as? Int
            
            completion(count: count ?? 0)
        
        }
    }
    
}
// MARK: - 发布微博
extension WBNetworkManager {
    
    /// 发布微博
    func postStatus(text: String, completion: (result: [String: AnyObject]?, isSuccess: Bool)->()) -> () {
        
        // 1. url
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        
        // 2. 参数字典
        let params = ["status": text]
        
        // 3. 发起网络请求
        tokenRequest(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            
            completion(result: json as? [String: AnyObject], isSuccess: isSuccess)

        }
    }
}
//MARK-用户信息
extension WBNetworkManager {

    //加载当前用户信息-用户登录后立即执行
    func loadUserInfo(completion: (dict: [String: AnyObject])->()) {
    
    
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        
        let params = ["uid": uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            completion(dict: (json as? [String: AnyObject]) ?? [:])
            
        }

    }

}

//MARK--OAuth相关方法
extension WBNetworkManager {

    //加载AccessToken
    func loadAccessToken(code: String, completion: (isSuccess: Bool)->()) {
    
        let  urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id": WBAppKey,
                      "client_secret": WBAppSecret,
                      "code": code,
                      "redirect_uri": WBRedirectURi]
        
        //发送网络请求
        request(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
//            print(json)
            //直接用字典设置userAccount属性
            self.userAccount.yy_modelSet(with: (json as? [String: AnyObject]) ?? [:])
            
            self.loadUserInfo(completion: { (dict) in
                self.userAccount.yy_modelSet(with: dict)
                print(self.userAccount)
                
                //保存模型
                self.userAccount.saveAccount()
                
                //完成回调
                completion(isSuccess: isSuccess)
            })
        }
    
    }
}
